package com.udf.hive.udf;

import com.udf.hive.unit.PropertiesUnits;
import org.apache.hadoop.hive.ql.exec.UDF;

import java.util.Properties;

public class IdNum2Address extends UDF {
    String proPath = "/id_num_addr_maping.properties";
    PropertiesUnits propertiesUnits = new PropertiesUnits();
    private static Properties idNumAddMap = null;

    public String evaluate(String input) {
        if (idNumAddMap == null || idNumAddMap.isEmpty()) {
            this.idNumAddMap = propertiesUnits.loadPro(proPath);
        }
        if (input != null) {
            String trimIdCode = input.trim();
            if (trimIdCode.length() == 18 || trimIdCode.length() == 15) {
                String iddrCode = trimIdCode.substring(0, 6);
                String addrStr = idNumAddMap.getProperty(iddrCode);
                if(addrStr!=null){
                    return addrStr;
                }
            }
        }
        return "";
    }
}